package net.topicopedia.other;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.*;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.appengine.api.datastore.Query.FilterOperator;

@SuppressWarnings("serial")
public class GetTopicInCategoriesServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		
		Query userQuery = new Query("Category");
		
		PreparedQuery pq = datastore.prepare(userQuery);

		resp.addHeader("Access-Control-Allow-Origin", "*");
		resp.setContentType("text/plain");
		
		String output = "[";
		
		for (Entity result : pq.asIterable() ) {
			
			if ( output.length() > 2 )
			{
				output += ", " ;
			}
			
			String storedUuid = result.getKey().getId() + "";
			String storedName = (String) result.getProperty("name");
			String storedCount = result.getProperty("count")+"";
		  
			output += "{ \"uuid\": \"" + storedUuid + "\", \"name\": \"" + storedName + "\", \"count\": \"" + storedCount + "\"}";
		}
		
		output += "]";
		 
		resp.getWriter().println( output );
		
	}
}
 